#pragma once

#include "Poller.h"
#include "Timestamp.h"


#include <vector>
#include <sys/epoll.h>


class Channel;

/**
 * 对epoll的封装
 * epoll_create
 * epoll_ctl   add/mod/del
 * epoll_wait
 */

class EpollPoller : public Poller
{
public:
    EpollPoller(EventLoop* loop);
    ~EpollPoller() override;

    // 重写基类Poller提供的纯虚方法
    Timestamp poll(int timeoutMs, ChannelList* activeChannels) override; // 封装epoll_wait
    // Channel update remove => EventLoop updateChannel removeChannel => Poller updateChannel removeChannel
    void updateChannel(Channel* channel) override;
    void removeChannel(Channel* channel) override;

private:
    // 处理epoll_wait返回的活跃的连接
    void fillActiveChannels(int numEvents, ChannelList* activeChannels) const;
    // 更新Channel epoll_ctl add/mod/del
    void update(int operation, Channel* channel);

private:
    using EventList = std::vector<epoll_event>;

    // epoll_event结构体数组的初始值
    static const int kInitEventListSize = 16;

    int m_epollfd;
    EventList m_events; // epoll返回时发生事件的数量
};